package com.zqxx.examine.management.controller.page;
 
import java.util.Collection;
import java.util.LinkedHashMap;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.zqxx.examine.common.utils.MenuItem;
import com.zqxx.examine.management.security.UserInfo;
import com.zqxx.examine.management.service.SystemService;
import com.zqxx.examine.management.service.UserService;

@Controller
public class BasePage
{
  @Autowired
  private UserService userService;
  @Autowired
  private SystemService systemService;
  
  @RequestMapping(value={"/"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  public String homePage(Model model, HttpServletRequest request)
  {
    return "redirect:home";
  }
  
  @RequestMapping(value={"/user-login-page"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  public String loginPage(Model model, @RequestParam(value="result", required=false, defaultValue="") String result)
  {
    if ("failed".equals(result)) {
      model.addAttribute("result", "无效的用户名或者密码");
    }
    return "login";
  }
  
  @RequestMapping(value={"/admin/home"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  public String adminHomePage(Model model, HttpServletRequest request)
  {
    return "redirect:/admin/dashboard";
  }
  
  @RequestMapping(value={"/teacher/home"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  public String teacherHomePage(Model model, HttpServletRequest request)
  {
    return "redirect:/teacher/dashboard";
  }
  
  @RequestMapping(value={"home"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  public String directToBaseHomePage(Model model, HttpServletRequest request)
  {
    if (SecurityContextHolder.getContext().getAuthentication() == null) {
      return "login";
    }
    if (SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString().endsWith("anonymousUser")) {
      return "login";
    }
    UserInfo userInfo = (UserInfo)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    Collection<? extends GrantedAuthority> grantedAuthorities = userInfo.getAuthorities();
    
    userInfo.setRoleMap(this.userService.getRoleMap());
    if (grantedAuthorities.contains(new SimpleGrantedAuthority("ROLE_ADMIN")))
    {
      LinkedHashMap<String, MenuItem> map = this.systemService.getMenuItemsByAuthority("ROLE_ADMIN");
      userInfo.setMenuMap(map);
      return "redirect:admin/home";
    }
    if (grantedAuthorities.contains(new SimpleGrantedAuthority("ROLE_TEACHER")))
    {
      LinkedHashMap<String, MenuItem> map = this.systemService.getMenuItemsByAuthority("ROLE_TEACHER");
      userInfo.setMenuMap(map);
      return "redirect:teacher/home";
    }
    if (grantedAuthorities.contains(new SimpleGrantedAuthority("ROLE_STUDENT"))) {
      return "login";
    }
    return "login";
  }
  
  @RequestMapping(value={"/user-detail/{userName}"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  public String userInfoPage(Model model, HttpServletRequest request, @PathVariable("userName") String userName)
  {
    return "redirect:/admin/home";
  }
  
  public static enum UserType
  {
    admin,  teacher,  student;
    
    private UserType() {}
  }
}
